import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToMany,
  JoinTable,
  OneToMany,
  OneToOne,
  JoinColumn,
  ManyToOne,
  Unique,
} from 'typeorm';
import { KnowledgeClassroom } from '@/entity/miniProgram/KnowledgeClassroom';
import { CapacityDiary } from './CapacityDiary';
interface VisitRecord {
  date: string | number;
  count: number;
}
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true })
  openid: string;

  /**
   * 患者：patient；亲属：kinsfolk；
   */
  @Column()
  role: 'kinsfolk' | 'patient';

  @Column({ nullable: true })
  avatar_url: string;

  @Column({ nullable: true, unique: false })
  phone: string;

  @Column()
  nick_name: string;

  @Column({ nullable: true })
  age: number;

  // 标签（自定义）
  @Column({ nullable: true })
  tag: string;

  /**
   * 性别:0无性别,1男,2女
   */
  @Column({ default: 0, nullable: true })
  gender: number;

  // 身高
  @Column({ nullable: true })
  height: number;

  // 体重
  @Column({ nullable: true })
  weight: number;

  // 饮食习惯
  @Column({ nullable: true })
  diet: string;

  /**
   * [{ date: string, count: number }]
   */
  @Column({ type: 'longtext', nullable: true })
  visit_list: string;

  // 初始干体质量（斤）
  @Column('float', { default: 0 })
  initial_dry_weight: number;

  // 初始静息心率（次/分）
  @Column('float', { default: 0 })
  initial_resting_heart_rate: number;

  // 模块点击数据
  @OneToMany(() => ModuleClick, (moduleClick) => moduleClick.user, {
    cascade: true, // 启用级联更新和删除
  })
  module_data: ModuleClick[];

  /**
   * 症状
   * 一个用户对应多个症状，一个症状也能对应多个用户
   */
  @ManyToMany(() => Symptom, (symptom) => symptom.user, {
    cascade: true, // 启用级联更新和删除
    eager: false, // 查找数据时自动返回该关联数据
  })
  @JoinTable({ name: 'user_symptom' })
  symptom: Symptom[];

  @OneToMany(() => DailyHealth, (health) => health.user, {
    cascade: true,
  })
  // @JoinTable({ name: 'user_daily_health' }) // 一对多的时候不需要
  daily_health: DailyHealth[];

  @OneToOne(
    () => InternalHealth,
    (internalHealth) => internalHealth.user,
    {
      nullable: true,
      cascade: true,
    }
  )
  internal_health: InternalHealth;

  @ManyToMany(() => RelationShip, (relative) => relative.user, {
    cascade: true,
  })
  @JoinTable({ name: 'user_relation_ship' })
  relation_ship: RelationShip[];

  @ManyToMany(
    () => KnowledgeClassroom,
    (knowledge) => knowledge.collect_user,
    {
      cascade: true,
      eager: false,
    }
  )
  @JoinTable({ name: 'user_collect_knowledge' })
  collect_knowledge: KnowledgeClassroom[];

  @OneToMany(() => CapacityDiary, (diary) => diary.user, {
    onDelete: 'CASCADE',
    // cascade: true,
  })
  capacity_diary: CapacityDiary;

  @Column({ default: false })
  delete: Boolean;

  @CreateDateColumn()
  created: Date;

  @UpdateDateColumn()
  updated: Date;
}

@Entity()
export class RelationShip {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToMany(() => User, (user) => user.relation_ship, {
    onDelete: 'CASCADE',
    // 自动返回关联的信息
    // eager: true,
  })
  // @JoinColumn({ name: 'user_id' })
  user: User[];

  @Column({ nullable: true })
  phone: string;

  // 患者id 即发起绑定的用户 id
  @Column({ nullable: true })
  patient_id: number;

  @Column({ default: '' })
  relation: string; // 前端定义：父母、女儿等

  @Column({ default: false })
  check_me_permission: boolean; // 简单的权限判断，是否可以查看我的数据
}

@Entity()
export class InternalHealth {
  @PrimaryGeneratedColumn()
  id: number;

  @OneToOne(() => User, (user) => user.internal_health, {
    onDelete: 'CASCADE', // 删除父实体时级联删除子实体外键
  })
  @JoinColumn({ name: 'user_id' })
  user: User;

  // 心衰类型，例如"射血分降低的心衰"
  @Column()
  heart_failure_type: string;

  // 心功能等级，例如"I级"
  @Column()
  heart_function_grade: string;

  // 用药史
  @Column({ type: 'text' })
  medication_history: string;

  // 手术史
  @Column({ type: 'text' })
  surgical_history: string;

  // 住院次数
  @Column({ default: 0 })
  hospitalizations: number;

  @CreateDateColumn()
  created: Date;

  @UpdateDateColumn()
  updated: Date;
}
@Entity()
export class DailyHealth {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(() => User, (user) => user.daily_health, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'user_id' })
  user: User;

  // 干体质量（斤）
  @Column('float', { default: 0 })
  dry_weight: number;

  // 静息心率（次/分）
  @Column('float', { default: 0 })
  resting_heart_rate: number;

  @Column('float', { default: 0 })
  today_weight: number; // 今日体质量（斤）

  @Column('float', { default: 0 })
  today_blood_pressure: number; // 今日血压（mmhg)

  @Column('float', { default: 0 })
  today_hypotension: number; // 今日低血压（mmhg)

  @Column('float', { default: 0 })
  today_resting_heart_rate: number; // 今日静息心率（次/分）

  @Column({ default: 0 })
  today_inability_to_lie_flat: number; // 不能平卧的程度

  @Column({ default: 0 })
  today_shortness_of_breath: number; // 呼吸困难的程度

  @Column({ default: 0 })
  today_fatigue: number; // 疲乏的程度

  @Column({ default: 0 })
  today_lower_limb_swelling: number; // 下肢水肿的程度

  @Column({ default: 0 })
  today_thirst: number; // 口渴的程度

  @Column({ default: 0 })
  today_abdominal_distension: number; // 腹胀的程度

  @Column({ default: 0 })
  today_poor_appetite: number; // 食欲不振的程度

  // 今日症状，0正常，1警告
  @Column({ default: 0 })
  today_status: number;

  // 0今日未提交，1今日已提交，
  @Column({ default: 0 })
  today_submit: number;

  // 备注
  @Column({ nullable: true })
  remarks: string;

  @CreateDateColumn()
  created: Date;

  @UpdateDateColumn()
  updated: Date;
}
@Entity()
export class Symptom {
  @PrimaryGeneratedColumn()
  id: number;
  /**
   * 症状名称
   */
  @Column({ unique: true })
  name: string;

  @ManyToMany(() => User, (user) => user.symptom, {
    onDelete: 'CASCADE',
  })
  user: User[];
}

@Entity()
export class ModuleClick {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(() => User, (user) => user.module_data, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'user_id' })
  user: User;

  // 模块名称
  @Column()
  module_name: string;

  // 访问次数
  @Column({ default: 0 })
  module_visit: number;
}
